package com.topica.network;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class CourseParse {
	private static final String PRE_COURSE_TABLE = "insert into course( course_id , info , link , name ) ";
	private static final String PRE_WEEK_TABLE = "insert into week( week_id , course_id ,quiz_id, name, practice, start_week, end_week ) ";
	private static final String PRE_LECTURE_TABLE = "insert into lecture( lecture_id , week_id , name ,  path_book, path_video ,  path_audio ) ";
	private static final String PRE_QUIZ_TABLE = "insert into quiz(id, quiz_id , quest_ids , num_question , time , name , time_open ,  time_close ) ";
	private static final String PRE_QUESTION_TABLE = "insert into question( question_id  , name , question_text , question_image) ";
	private static final String PRE_ANSWER_TABLE = "insert into answer(id, question_id ,  fraction , answer ) ";

	public static ArrayList<String> parseCourse(String data)
			throws JSONException {
		ArrayList<String> rs = new ArrayList<String>();
		JSONObject json;
		Log.i("CourseData", "data = " + data);
		json = new JSONObject(data);
		JSONArray infoCourse = json.getJSONArray("infocourse");

		JSONObject opCourse = infoCourse.getJSONObject(0);
		JSONArray arCourse = opCourse.getJSONArray("course");

		for (int i = 0, n = arCourse.length(); i < n; i++) {
			JSONObject valueCourse = arCourse.getJSONObject(i);
			rs.add(PRE_COURSE_TABLE + valueCourse.getString("value"));
		}

		JSONObject opWeek = infoCourse.getJSONObject(1);
		JSONArray arWeek = opWeek.getJSONArray("week");
		JSONObject valueWeek;

		for (int i = 0, n = arWeek.length(); i < n; i++) {
			valueWeek = arWeek.getJSONObject(i);
			rs.add(PRE_WEEK_TABLE + valueWeek.getString("value"));
		}

		JSONObject opLecture = infoCourse.getJSONObject(2);
		JSONArray arLecture = opLecture.getJSONArray("lecture");
		JSONObject valueLecture;
		for (int i = 0, n = arLecture.length(); i < n; i++) {
			valueLecture = arLecture.getJSONObject(i);
			rs.add(PRE_LECTURE_TABLE + valueLecture.getString("value"));
		}

		JSONObject opQuiz = infoCourse.getJSONObject(3);
		JSONArray arQuiz = opQuiz.getJSONArray("quiz");
		JSONObject valueQuiz;
		for (int i = 0, n = arQuiz.length(); i < n; i++) {
			valueQuiz = arQuiz.getJSONObject(i);
			rs.add(PRE_QUIZ_TABLE + valueQuiz.getString("value"));
		}

		JSONObject opQuestion = infoCourse.getJSONObject(4);
		JSONArray arQuestion = opQuestion.getJSONArray("question");
		JSONObject valueQuestion;

		for (int i = 0, n = arQuestion.length(); i < n; i++) {
			valueQuestion = arQuestion.getJSONObject(i);
			rs.add(PRE_QUESTION_TABLE + valueQuestion.getString("value"));
		}

		JSONObject opAnswer = infoCourse.getJSONObject(5);
		JSONArray arAnswer = opAnswer.getJSONArray("answer");
		JSONObject valueAnswer;
		for (int i = 0, n = arAnswer.length(); i < n; i++) {
			valueAnswer = arAnswer.getJSONObject(i);
			rs.add(PRE_ANSWER_TABLE + valueAnswer.getString("value"));
		}

		return rs;
	}
}
